package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.StoreConstants;
import com.powernode.domain.Notice;
import com.powernode.mapper.NoticeMapper;
import com.powernode.service.NoticeService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 贺
* @description 针对表【notice】的数据库操作Service实现
* @createDate 2024-07-19 13:40:40
*/
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.NoticeServiceImpl")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{

    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 新增公告
     * @Param notice
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    public Boolean saveNotice(Notice notice) {
        // 补充公告对象属性
        notice.setShopId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        return noticeMapper.insert(notice) > 0;
    }

    /**
     * 修改公告
     *
     * @param notice
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    public Boolean modifyNotice(Notice notice) {
        // 补充公告对象属性
        notice.setUpdateTime(new Date());
        return noticeMapper.updateById(notice) > 0;
    }

    /**
     * 删除公告
     *
     * @param noticeId
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    public Boolean removeNoticeById(Long noticeId) {
        return noticeMapper.deleteById(noticeId) > 0;
    }

    /**
     * 查询小程序置顶公告
     *
     * @return
     */
    @Override
    @Cacheable(key = StoreConstants.WX_TOP_NOTICE_LIST_KEY)
    public List<Notice> queryWxTopNoticeList() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus, 1)
                .eq(Notice::getIsTop, 1)
                .orderByDesc(Notice::getCreateTime));
    }

    /**
     * 查询小程序所有公告列表
     *
     * @return
     */
    @Override
    @Cacheable(key = StoreConstants.WX_ALL_NOTICE_LIST_KEY)
    public List<Notice> queryWxAllNoticeList() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus, 1)
                .orderByDesc(Notice::getCreateTime));
    }
}




